package com.huatai.exchange.send;

import com.faneui.common.exception.BusinessException;
import com.huatai.exchange.facade.CommunicationConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("fileMailSendService")
public class FileMailSendServiceImpl extends AbstractFileSendServiceImpl implements FileSendService {

    private final static String error_tip="sql执行异常" ;

    @Override
    public void sendMessage(String destAddress, Map<String, Object> resvMap) throws BusinessException {
        boolean b = resvMap.get("fileNames") == null || StringUtils.isBlank(resvMap.get("fileNames").toString());
        String fileNames = b ? null : resvMap.get("fileNames").toString();
        String filePaths = b ? null : resvMap.get("filePaths").toString();

        String toCcMails = (String) resvMap.get(CommunicationConstants.MAIL_toCcMails);
        String toCcSeMails = (String) resvMap.get(CommunicationConstants.MAIL_toSeCcMails);
        String mailTitle = formatStr((String) resvMap.get(CommunicationConstants.MAIL_TITLE), resvMap);
        String mailContent = (String) resvMap.get(CommunicationConstants.MAIL_CONTENT);
        if(mailContent.contains(error_tip)) {
            throw new BusinessException("处理失败：邮件内容sql执行异常") ;
        }
        // 秘密抄送 监控邮件
        if(StringUtils.isBlank(toCcSeMails)){
            toCcSeMails = monitorMail;
        } else {
            toCcSeMails = toCcSeMails + "," + monitorMail ;
        }

        String businId = (String) resvMap.get("businId") ;
        communicationService.sendMail(businId,mailTitle, mailContent, destAddress, fileNames, filePaths, toCcMails, toCcSeMails);

    }
}
